package com.kc.business.service.impl;

import com.kc.business.domain.KcFreeTicketApply;
import com.kc.business.domain.vo.KcFreeTicketApplyVo;
import com.kc.business.enums.FreeTicketApplyStatusEnum;
import com.kc.business.mapper.BizFreeTicketMapper;
import com.kc.business.service.IBizFreeTicketService;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * FileName: BizFreeTicketServiceImpl
 * Author:   TP
 * Date:     2020-10-28 14:10
 * Description: 免票申请服务
 */
@Service
public class BizFreeTicketServiceImpl implements IBizFreeTicketService {

    @Autowired
    private BizFreeTicketMapper bizFreeTicketMapper;

    @Override
    public List<KcFreeTicketApply> getList(KcFreeTicketApplyVo vo) {
        return bizFreeTicketMapper.getList(vo);
    }

    @Override
    public AjaxResult audit(Integer id, Integer status, String rejectReason) {
        AjaxResult checkResult = auditCheckPre(status, rejectReason);
        if (null != checkResult) {
            return checkResult;
        }
        KcFreeTicketApply toUpdateApply = bizFreeTicketMapper.getById(id);
        if (toUpdateApply != null) {
            if (!FreeTicketApplyStatusEnum.AUDITING.getStatus().equals(toUpdateApply.getAuditStatus())) {
                return AjaxResult.error(String.format("此条申请当前状态已为[%s]，请刷新重试",
                        FreeTicketApplyStatusEnum.getMessage(toUpdateApply.getAuditStatus())));
            }
            if (bizFreeTicketMapper.audit(id, status, rejectReason) > 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，更新数据失败");
        }
        return AjaxResult.error("操作失败，未找到相关数据项");
    }

    @Override
    public AjaxResult auditBatch(List<Integer> ids, Integer status, String rejectReason) {
        AjaxResult checkResult = auditCheckPre(status, rejectReason);
        if (null != checkResult) {
            return checkResult;
        }
        // 查询需要审批的免票申请记录
        List<KcFreeTicketApply> toAuditApplies = bizFreeTicketMapper.getByIds(ids);
        if (CollectionUtils.isNotEmpty(toAuditApplies)) {
            List<KcFreeTicketApply> changedStatusApplies = toAuditApplies.stream().filter(e ->
                    !FreeTicketApplyStatusEnum.AUDITING.getStatus().equals(e.getAuditStatus())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(changedStatusApplies)) {
                // 说明存在状态已变更
                return AjaxResult.error("操作失败，部分记录状态已变更，请刷新重试");
            }
            if (bizFreeTicketMapper.auditBatch(ids, status, rejectReason) > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("操作失败，更新数据失败");
            }
        }
        return AjaxResult.error("操作失败，未查询到相关数据项");
    }

    private AjaxResult auditCheckPre(Integer status, String rejectReason) {
        if (!FreeTicketApplyStatusEnum.getAllStatus().contains(status)) {
            return AjaxResult.error("操作失败：审核状态无效");
        }
        if (FreeTicketApplyStatusEnum.AUDIT_REJECT.getStatus().equals(status)) {
            if (StringUtils.isEmpty(rejectReason)) {
                return AjaxResult.error("操作失败：审核拒绝必须填写拒绝原因");
            }
        }
        return null;
    }
}
